package org.cp.dao.admin.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.cp.dao.BaseDAO;
import org.cp.dao.admin.IRoomDAO;
import org.cp.vo.Room;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class RoomDAOImpl extends BaseDAO implements IRoomDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(RoomDAOImpl.class);

	/*
	 * (non-Javadoc)
	 *
	 * @see org.cp.dao.admin.impl.IRoomDAO#addRoom(org.cp.vo.Room)
	 */
	public void addRoom(Room room) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(room);
		tx.commit();
		session.close();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.cp.dao.admin.impl.IRoomDAO#deleteRoom(org.cp.vo.Room)
	 */
	public void deleteRoom(Room room) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.delete(room);
		tx.commit();
		session.close();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.cp.dao.admin.impl.IRoomDAO#updateRoom(org.cp.vo.Room)
	 */
	public void updateRoom(Room room) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.update(room);
		tx.commit();
		session.close();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.cp.dao.admin.impl.IRoomDAO#updateRoomByHql(java.lang.String)
	 */
	public int updateRoomByHql(String hql) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		int i = session.createQuery(hql).executeUpdate();
		tx.commit();
		session.close();
		return i;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.cp.dao.admin.impl.IRoomDAO#listRoom()
	 */
	public List listRoom() {
		Session session = getSession();
		List list = session.createQuery("from Room").list();
		session.close();
		return list;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.cp.dao.admin.impl.IRoomDAO#queryRoomByHql(java.lang.String)
	 */
	public List queryRoomByHql(String hql) {
		Session session = getSession();
		List numbers = session.createQuery(hql).list();
		session.close();
		if (numbers.size() != 0) {
			return numbers;
		}
		return null;
	}
}
